{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Minimum Consecutive Cards to Pick Up"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Medium"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array #hash-table #sliding-window"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组 #哈希表 #滑动窗口"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: minimumCardPickup"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #必须拿起的最小连续卡牌数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个整数数组 <code>cards</code> ，其中 <code>cards[i]</code> 表示第 <code>i</code> 张卡牌的 <strong>值</strong> 。如果两张卡牌的值相同，则认为这一对卡牌 <strong>匹配</strong> 。</p>\n",
    "\n",
    "<p>返回你必须拿起的最小连续卡牌数，以使在拿起的卡牌中有一对匹配的卡牌。如果无法得到一对匹配的卡牌，返回 <code>-1</code> 。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>cards = [3,4,2,3,4,7]\n",
    "<strong>输出：</strong>4\n",
    "<strong>解释：</strong>拿起卡牌 [3,4,2,3] 将会包含一对值为 3 的匹配卡牌。注意，拿起 [4,2,3,4] 也是最优方案。</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>cards = [1,0,5,3]\n",
    "<strong>输出：</strong>-1\n",
    "<strong>解释：</strong>无法找出含一对匹配卡牌的一组连续卡牌。</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= cards.length &lt;= 10<sup>5</sup></code></li>\n",
    "\t<li><code>0 &lt;= cards[i] &lt;= 10<sup>6</sup></code></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [minimum-consecutive-cards-to-pick-up](https://leetcode.cn/problems/minimum-consecutive-cards-to-pick-up/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [minimum-consecutive-cards-to-pick-up](https://leetcode.cn/problems/minimum-consecutive-cards-to-pick-up/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[3,4,2,3,4,7]', '[1,0,5,3]']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumCardPickup(self, cards: List[int]) -> int:\n",
    "        \n",
    "        if len(cards) == 1:\n",
    "            return -1\n",
    "\n",
    "        min_len = float('inf')\n",
    "        sliding_window = set()\n",
    "        start, end = 0,1\n",
    "        sliding_window.add(cards[0])\n",
    "        while end < len(cards):\n",
    "            while cards[end] in sliding_window:\n",
    "                if min_len > len(sliding_window) + 1:\n",
    "                    min_len = len(sliding_window) + 1\n",
    "                sliding_window.discard(cards[start])\n",
    "                start +=1\n",
    "            sliding_window.add(cards[end])\n",
    "            end +=1\n",
    "        \n",
    "        return min_len if min_len < float('inf') else -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumCardPickup(self, cards: List[int]) -> int:\n",
    "        res = inf\n",
    "        left = 0\n",
    "        right = 0\n",
    "        visited = set()\n",
    "        while right < len(cards):\n",
    "            num = cards[right]\n",
    "            while num in visited:\n",
    "                res = min(res, right - left + 1)\n",
    "                visited.remove(cards[left])\n",
    "                left += 1\n",
    "            visited.add(num)\n",
    "            right += 1\n",
    "        return res if res != inf else -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumCardPickup(self, cards: List[int]) -> int:\n",
    "        n = len(cards)\n",
    "        left, right = 0, 0\n",
    "        visited = set()\n",
    "        res = float('inf')\n",
    "        while right < n:\n",
    "            num = cards[right]\n",
    "            # 如果num已经在滑窗内\n",
    "            while num in visited:\n",
    "                # 更新res\n",
    "                res = min(res, right - left + 1)\n",
    "                # 收缩左边界\n",
    "                visited.remove(cards[left])\n",
    "                left += 1\n",
    "            # 加入当前数字\n",
    "            visited.add(num)\n",
    "            # 移动右边界\n",
    "            right += 1\n",
    "        if res == float('inf'):\n",
    "            return -1\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:    \n",
    "    def minimumCardPickup(self, cards: List[int]) -> int:\n",
    "        n = len(cards)\n",
    "        left, right = 0, 0\n",
    "        visited = set()\n",
    "        res = float('inf')\n",
    "        while right < n:\n",
    "            num = cards[right]\n",
    "            # 如果num已经在滑窗内\n",
    "            while num in visited:\n",
    "                # 更新res\n",
    "                res = min(res, right - left + 1)\n",
    "                # 收缩左边界\n",
    "                visited.remove(cards[left])\n",
    "                left += 1\n",
    "            # 加入当前数字\n",
    "            visited.add(num)\n",
    "            # 移动右边界\n",
    "            right += 1\n",
    "        if res == float('inf'):\n",
    "            return -1\n",
    "        return res\n",
    "\n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumCardPickup(self, cards: List[int]) -> int:\n",
    "        l=0\n",
    "        size=len(cards)\n",
    "        ans=float('inf')\n",
    "        visited=set([cards[0]])\n",
    "        for r in range(1,size):\n",
    "            while cards[r] in visited:\n",
    "\n",
    "                visited.remove(cards[l])\n",
    "\n",
    "                l+=1\n",
    "                l_ans=r-(l-1)+1\n",
    "                ans=min(ans,l_ans)\n",
    "            visited.add(cards[r])\n",
    "        if ans==float('inf'):\n",
    "            return -1         \n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumCardPickup(self, cards: List[int]) -> int:\n",
    "        if not cards or len(cards) == 0:\n",
    "            return -1\n",
    "        n = len(cards)\n",
    "        l, r = 0, 0\n",
    "        window = set()\n",
    "        res = float('inf')\n",
    "        while r < n:\n",
    "            c = cards[r]\n",
    "            r = r + 1\n",
    "            while c in window:\n",
    "                res = min(res, r - l)\n",
    "                d = cards[l]\n",
    "                l = l + 1\n",
    "                window.remove(d)\n",
    "            window.add(c)\n",
    "        return res if res != float('inf') else -1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumCardPickup(self, cards: List[int]) -> int:\n",
    "        st = set()\n",
    "        ans ,left = inf, 0\n",
    "        for right, x in enumerate(cards):\n",
    "            if x in st:\n",
    "                while cards[left] != x:\n",
    "                    st.discard(cards[left])\n",
    "                    left += 1\n",
    "                ans = min(ans, right - left + 1)\n",
    "                st.discard(cards[left])\n",
    "                left += 1\n",
    "            st.add(x)\n",
    "        return ans if ans != inf else -1\n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumCardPickup(self, cards: List[int]) -> int:\n",
    "        n = len(cards)\n",
    "        if n == 1:\n",
    "            return -1 \n",
    "        li = set([cards[0]])\n",
    "        left,right = 0,1 \n",
    "        ans =  inf \n",
    "        while right < n:\n",
    "            \n",
    "            while cards[right] in li and left < right:\n",
    "                ans = min(right - left + 1,ans)\n",
    "                li.remove(cards[left])\n",
    "                left += 1 \n",
    "            \n",
    "            li.add(cards[right])\n",
    "            \n",
    "            \n",
    "            right += 1\n",
    "                \n",
    "        return -1 if ans > n else ans \n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumCardPickup(self, cards: List[int]) -> int:\n",
    "        res = len(cards) + 1\n",
    "        counter = defaultdict(int)\n",
    "        left = 0\n",
    "        for right in range(len(cards)):\n",
    "            counter[cards[right]] += 1\n",
    "            if counter[cards[right]] == 1:\n",
    "                continue\n",
    "            while counter[cards[right]] > 1:\n",
    "                counter[cards[left]] -= 1\n",
    "                left += 1\n",
    "            res = min(res, right - left + 2)\n",
    "\n",
    "        return res if res != len(cards) + 1 else -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumCardPickup(self, cards: List[int]) -> int:\n",
    "        left, right = 0, 0\n",
    "        window = defaultdict(lambda: 0)\n",
    "\n",
    "        cnt = 0\n",
    "\n",
    "        res = float(\"inf\")\n",
    "\n",
    "        while right < len(cards):\n",
    "            c = cards[right]\n",
    "            right += 1\n",
    "\n",
    "            window[c] += 1\n",
    "\n",
    "            if window[c] == 2:\n",
    "                cnt += 1\n",
    "        \n",
    "            if cnt == 1:\n",
    "                res = min(res, right - left)\n",
    "            # 缩紧左边界\n",
    "            while cnt >= 1 and left < right:\n",
    "                d = cards[left]\n",
    "                left += 1\n",
    "\n",
    "                window[d] -= 1\n",
    "\n",
    "                if window[d] == 1:\n",
    "                    cnt -= 1\n",
    "            \n",
    "                if cnt == 1:\n",
    "                    res = min(res, right - left)\n",
    "\n",
    "        return res if res != float(\"inf\") else -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumCardPickup(self, cards: List[int]) -> int:\n",
    "        d, res, l = defaultdict(int), inf, 0\n",
    "        for r, c in enumerate(cards):\n",
    "            d[c] += 1\n",
    "            while d[c] > 1:\n",
    "                res = min(res, r - l + 1)\n",
    "                d[cards[l]] -= 1\n",
    "                l += 1\n",
    "        return res if res != inf else -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumCardPickup(self, cards: List[int]) -> int:\n",
    "        left = 0\n",
    "        ans = inf\n",
    "        cnt = Counter()\n",
    "\n",
    "        for right,x in enumerate(cards):\n",
    "            cnt[x] += 1\n",
    "            while cnt[x] > 1:\n",
    "                ans = min(right - left + 1,ans)\n",
    "                cnt[cards[left]] -= 1\n",
    "                if cnt[cards[left]] == 0:\n",
    "                    del cnt[cards[left]]\n",
    "                left += 1\n",
    "        return ans if ans < inf else -1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumCardPickup(self, cards: list[int]) -> int:\n",
    "        from collections import defaultdict\n",
    "        curWin = defaultdict(int)\n",
    "        start = 0\n",
    "        minL = float(\"inf\")\n",
    "        for end in range(len(cards)):\n",
    "            curWin[cards[end]] += 1\n",
    "            while curWin[cards[end]] > 1:\n",
    "                minL = min(minL, end - start + 1)\n",
    "                curWin[cards[start]] -= 1\n",
    "                start += 1\n",
    "        return -1 if minL == float(\"inf\") else minL"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumCardPickup(self, cards: List[int]) -> int:\n",
    "        n = len(cards)\n",
    "        cnt = Counter()\n",
    "        ans, l = inf, 0\n",
    "        for r, x in enumerate(cards):\n",
    "            cnt[x] += 1\n",
    "            while cnt[x] > 1:\n",
    "                ans = min(ans, r - l + 1)\n",
    "                cnt[cards[l]] -= 1\n",
    "                l += 1\n",
    "        return ans if ans != inf else -1\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumCardPickup(self, cards: List[int]) -> int:\n",
    "        ans = inf\n",
    "        indices = defaultdict(int)\n",
    "        for i in range(len(cards)):\n",
    "            if cards[i] in indices:\n",
    "                ans = min(ans,i-indices[cards[i]]+1)\n",
    "            indices[cards[i]] = i\n",
    "        \n",
    "        return ans if ans != inf else -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumCardPickup(self, cards: List[int]) -> int:\n",
    "       q=Counter()\n",
    "       ans=inf\n",
    "       i=0\n",
    "       for x in cards:\n",
    "           if x in q:\n",
    "               ans=min(ans,i-q[x]+1)\n",
    "           q[x]=i\n",
    "           i+=1\n",
    "       return ans if ans!=inf else -1         "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution(object):\n",
    "    def minimumCardPickup(self, cards):\n",
    "        \"\"\"\n",
    "        :type cards: List[int]\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "\n",
    "        dct, ans = {}, len(cards) + 1\n",
    "        for i, card in enumerate(cards):\n",
    "            if card in dct: \n",
    "                ans = min(ans, i - dct[card] + 1)\n",
    "            dct[card] = i\n",
    "        return -1 if ans == len(cards) + 1 else ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumCardPickup(self, cards: List[int]) -> int:\n",
    "        from collections import defaultdict\n",
    "        counts = {}\n",
    "        res = float(\"inf\")\n",
    "        for i,c in enumerate(cards):\n",
    "            if c in counts.keys():\n",
    "                res = min(res,i-counts[c]+1)\n",
    "                counts[c] = i\n",
    "            else:\n",
    "                counts[c] = i\n",
    "        if res == float(\"inf\"):\n",
    "            return -1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumCardPickup(self, cards: List[int]) -> int:\n",
    "        c = Counter()\n",
    "        ans = inf\n",
    "        for i, n in enumerate(cards):\n",
    "            if n in c:\n",
    "                ans = min(ans, i - c[n])\n",
    "            c[n] = i\n",
    "        return -1 if ans == inf else ans + 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumCardPickup(self, cards: List[int]) -> int:\n",
    "        ans, dic = 1000005, dict()\n",
    "        for i, num in enumerate(cards):\n",
    "            if num in dic:\n",
    "                ans = min(ans, i - dic[num] + 1)\n",
    "            dic[num] = i\n",
    "        return ans if ans != 1000005 else -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution(object):\n",
    "    def minimumCardPickup(self, cards):\n",
    "        \"\"\"\n",
    "        :type cards: List[int]\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "\n",
    "        dct, ans = {}, len(cards) + 1\n",
    "        for i, card in enumerate(cards):\n",
    "            if card in dct: \n",
    "                ans = min(ans, i - dct[card] + 1)\n",
    "            dct[card] = i\n",
    "        return -1 if ans == len(cards) + 1 else ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumCardPickup(self, cards: List[int]) -> int:\n",
    "        mp = {}\n",
    "        ans = inf\n",
    "        for right, x in enumerate(cards):\n",
    "            if x not in mp:\n",
    "                mp[x] = right\n",
    "            else:\n",
    "                ans = min(ans, right - mp[x] + 1)\n",
    "                mp[x] = right\n",
    "        return ans if ans != inf else -1\n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumCardPickup(self, cards: List[int]) -> int:\n",
    "        ans, dic = 1000005, dict()\n",
    "        for i, num in enumerate(cards):\n",
    "            if num in dic.keys():\n",
    "                ans = min(ans, i - dic[num] + 1)\n",
    "            dic[num] = i\n",
    "        return ans if ans != 1000005 else -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumCardPickup(self, cards: List[int]) -> int:\n",
    "        d=dict()\n",
    "        ans=100001\n",
    "        for i,item in enumerate(cards):\n",
    "            if (item in d):\n",
    "                ans=min(ans,i-d[item]+1)\n",
    "            d[item]=i\n",
    "        return -1 if ans==100001 else ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumCardPickup(self, cards: List[int]) -> int:\n",
    "        d = {}\n",
    "        res = 100001\n",
    "        for i in range(len(cards)):\n",
    "            if cards[i] in d:\n",
    "                tmp = d[cards[i]]\n",
    "                res = min(res,i - tmp + 1)\n",
    "                d[cards[i]] = i\n",
    "            else:\n",
    "                d[cards[i]] = i\n",
    "        return -1 if res == 100001 else res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumCardPickup(self, cards: List[int]) -> int:\n",
    "        d = {}\n",
    "        res = 100001\n",
    "        for i in range(len(cards)):\n",
    "            if cards[i] in d:\n",
    "                tmp = d[cards[i]]\n",
    "                res = min(res,i - tmp + 1)\n",
    "                d[cards[i]] = i\n",
    "            else:\n",
    "                d[cards[i]] = i\n",
    "        return -1 if res == 100001 else res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumCardPickup(self, cards: List[int]) -> int:\n",
    "        字典 = dict()\n",
    "        最小连续数 = float('inf')\n",
    "        for i, j in enumerate(cards):\n",
    "            if j in 字典:\n",
    "                最小连续数 = min(最小连续数, i-字典[j])\n",
    "                字典[j] = i\n",
    "            else:\n",
    "                字典[j] = i\n",
    "        return -1 if 最小连续数 == float('inf') else 最小连续数+1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumCardPickup(self, cards: List[int]) -> int:\n",
    "        c = dict()\n",
    "        ans = inf\n",
    "        for i, n in enumerate(cards):\n",
    "            if n in c:\n",
    "                ans = min(ans, i - c[n])\n",
    "            c[n] = i\n",
    "        return -1 if ans == inf else ans + 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumCardPickup(self, cards: List[int]) -> int:\n",
    "        c = Counter()\n",
    "        ans = inf\n",
    "        for i, n in enumerate(cards):\n",
    "            if n in c:\n",
    "                ans = min(ans, i - c[n])\n",
    "            c[n] = i\n",
    "        return -1 if ans == inf else ans + 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumCardPickup(self, cards: List[int]) -> int:\n",
    "        字典 = dict()\n",
    "        最小连续数 = float('inf')\n",
    "        for i, j in enumerate(cards):\n",
    "            if j in 字典:\n",
    "                最小连续数 = min(最小连续数, i-字典[j])\n",
    "                字典[j] = i\n",
    "            else:\n",
    "                字典[j] = i\n",
    "        return -1 if 最小连续数 == float('inf') else 最小连续数+1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumCardPickup(self, cards: List[int]) -> int:\n",
    "        字典 = dict()\n",
    "        最小连续数 = float('inf')\n",
    "        for i, j in enumerate(cards):\n",
    "            if j in 字典:\n",
    "                最小连续数 = min(最小连续数, i-字典[j])\n",
    "                字典[j] = i\n",
    "            else:\n",
    "                字典[j] = i\n",
    "        return -1 if 最小连续数 == float('inf') else 最小连续数+1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumCardPickup(self, cards: List[int]) -> int:\n",
    "        d=dict()\n",
    "        ans=100001\n",
    "        for i,item in enumerate(cards):\n",
    "            if (item in d):\n",
    "                ans=min(ans,i-d[item]+1)\n",
    "            d[item]=i\n",
    "        return -1 if ans==100001 else ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumCardPickup(self, cards: List[int]) -> int:\n",
    "        字典 = dict()\n",
    "        最小连续数 = float('inf')\n",
    "        for i, j in enumerate(cards):\n",
    "            if j in 字典:\n",
    "                最小连续数 = min(最小连续数, i-字典[j])\n",
    "                字典[j] = i\n",
    "            else:\n",
    "                字典[j] = i\n",
    "        return -1 if 最小连续数 == float('inf') else 最小连续数+1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumCardPickup(self, cards: List[int]) -> int:\n",
    "        d = dict()\n",
    "        ans = 100001\n",
    "        for i, item in enumerate(cards):\n",
    "            if (item in d):\n",
    "                ans = min(ans, i - d[item] + 1)\n",
    "            d[item] = i \n",
    "        return -1 if ans == 100001 else ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumCardPickup(self, cards: List[int]) -> int:\n",
    "        字典 = dict()\n",
    "        最小连续数 = float('inf')\n",
    "        for i, j in enumerate(cards):\n",
    "            if j in 字典:\n",
    "                最小连续数 = min(最小连续数, i-字典[j])\n",
    "                字典[j] = i\n",
    "            else:\n",
    "                字典[j] = i\n",
    "        return -1 if 最小连续数 == float('inf') else 最小连续数+1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumCardPickup(self, cards: List[int]) -> int:\n",
    "        字典 = dict()\n",
    "        最小连续数 = float('inf')\n",
    "        for i, j in enumerate(cards):\n",
    "            if j in 字典:\n",
    "                最小连续数 = min(最小连续数, i-字典[j])\n",
    "            字典[j] = i\n",
    "        return -1 if 最小连续数 == float('inf') else 最小连续数+1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumCardPickup(self, cards: List[int]) -> int:\n",
    "        out = len(cards) + 1000\n",
    "        last = [-1] * (max(cards) + 1)\n",
    "\n",
    "        flag = False\n",
    "\n",
    "        for idx, num in enumerate(cards):\n",
    "            if last[num] == -1:\n",
    "                last[num] = idx\n",
    "            else:\n",
    "                flag = True\n",
    "                if (idx - last[num] + 1) < out:\n",
    "                    out = idx - last[num] + 1\n",
    "                last[num] = idx\n",
    "                \n",
    "        if flag:\n",
    "            return out\n",
    "        else: \n",
    "            return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumCardPickup(self, cards: List[int]) -> int:\n",
    "        ans = len(cards) + 1\n",
    "        m = dict()\n",
    "\n",
    "        for i, v in enumerate(cards):\n",
    "            if v not in m.keys():\n",
    "                m[v] = i\n",
    "            else:\n",
    "                ans = min(ans, i - m[v] + 1)\n",
    "                m[v] = i\n",
    "\n",
    "        if ans == len(cards) + 1:\n",
    "            return -1\n",
    "        else:\n",
    "            return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumCardPickup(self, cards: List[int]) -> int:\n",
    "        card_count = {}\n",
    "        min_pickup = float('inf')\n",
    "\n",
    "        for i, card in enumerate(cards):\n",
    "            if card in card_count:\n",
    "                min_pickup = min(min_pickup, i - card_count[card] + 1)\n",
    "            card_count[card] = i\n",
    "\n",
    "        return min_pickup if min_pickup != float('inf') else -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumCardPickup(self, cards: List[int]) -> int:\n",
    "        checking = {}\n",
    "        card = -1\n",
    "        for i in range(len(cards)):\n",
    "            if card == 2:\n",
    "                return card\n",
    "            ele = cards[i]\n",
    "            if ele in checking:\n",
    "                length = i - checking[ele] + 1\n",
    "                card = min(card, length) if not card == -1 else length\n",
    "            checking[ele] = i\n",
    "        return card"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumCardPickup(self, cards: List[int]) -> int:\n",
    "        d = dict()\n",
    "        ans = inf\n",
    "        for i, x in enumerate(cards):\n",
    "            if d.get(x, -1) != -1:\n",
    "                ans = min(ans, i - d[x] + 1)\n",
    "            d[x] = i\n",
    "        if ans == inf:\n",
    "            ans = -1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumCardPickup(self, cards: List[int]) -> int:\n",
    "        que = dict()\n",
    "        ans = inf\n",
    "        for i, j in enumerate(cards):\n",
    "            if j in que and i - que[j] + 1 < ans:\n",
    "                ans = i - que[j] + 1\n",
    "            que[j] = i \n",
    "        return ans if ans != inf else -1\n",
    "         \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumCardPickup(self, cards: List[int]) -> int:\n",
    "        '''\n",
    "            从左向右遍历，记录遇到的每个数的最大索引\n",
    "        '''\n",
    "        n = len(cards)\n",
    "        idx = {}\n",
    "        res = n+1\n",
    "        for i, x in enumerate(cards):\n",
    "            res = min(res, i-idx.get(x, -n-2)+1)\n",
    "            idx[x] = i\n",
    "        return res if res < n + 1 else -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumCardPickup(self, cards: List[int]) -> int:\n",
    "        minimum = float('inf')\n",
    "        d = dict()\n",
    "        for i,num in enumerate(cards):\n",
    "            if num in d:\n",
    "                minimum = min(minimum,i-d[num]+1)\n",
    "            d[num]=i\n",
    "        return -1 if minimum == float('inf') else minimum    \n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumCardPickup(self, cards: List[int]) -> int:\n",
    "        d = {}\n",
    "        res = len(cards) + 1\n",
    "        for idx, c in enumerate(cards):\n",
    "            if c in d.keys():\n",
    "                res = min(idx - d[c] + 1, res)\n",
    "                d[c] = idx\n",
    "            else:\n",
    "                d[c] = idx\n",
    "        return -1 if res == len(cards) + 1 else res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumCardPickup(self, cards: List[int]) -> int:\n",
    "        ans = inf\n",
    "        visited = {}\n",
    "        for i, num in enumerate(cards):\n",
    "            if num in visited and ans > (dist := i-visited[num]+1):\n",
    "                ans = dist\n",
    "            visited[num] = i \n",
    "        return ans if ans < inf else -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumCardPickup(self, cards: List[int]) -> int:\n",
    "\n",
    "        card_map = {}  # 用来存储卡牌值和它们出现的最新索引\n",
    "        min_length = float('inf')  # 用一个很大的数开始，代表目前还没有找到匹配\n",
    "\n",
    "        for i, card in enumerate(cards):\n",
    "            if card in card_map:\n",
    "                # 计算两个匹配卡牌之间的距离\n",
    "                length = i - card_map[card] + 1\n",
    "                # 如果当前的距离更短，就更新最小长度\n",
    "                min_length = min(min_length, length)\n",
    "            card_map[card] = i  # 更新当前卡牌的索引\n",
    "\n",
    "        # 如果找到了匹配的卡牌对，返回最小长度，否则返回-1\n",
    "        return min_length if min_length != float('inf') else -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumCardPickup(self, cards: List[int]) -> int:\n",
    "        d = dict()\n",
    "        mn = float('inf')\n",
    "        for i, q in enumerate(cards):\n",
    "            if q in d:\n",
    "                mn = min(mn, i + 1 - d[q])\n",
    "            d[q] = i\n",
    "        \n",
    "        return -1 if mn == float('inf') else mn"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumCardPickup(self, cards: List[int]) -> int:\n",
    "        di = dict()\n",
    "        ans = len(cards) + 1\n",
    "        for i,x in enumerate(cards):\n",
    "            if x in di: ans = min(ans,i-di[x]+1)\n",
    "            di[x] = i\n",
    "        return ans if ans != len(cards) + 1 else -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    # # 贪心+哈希表\n",
    "    def minimumCardPickup(self, cards: List[int]) -> int:\n",
    "        ans = len(cards) + 1\n",
    "        pos = {}\n",
    "\n",
    "        for i, v in enumerate(cards):\n",
    "            if v in pos:\n",
    "                p = pos[v]\n",
    "                if i - p + 1 < ans:  # 找最近出现的 v\n",
    "                    ans = i - p + 1\n",
    "\n",
    "            pos[v] = i\n",
    "\n",
    "        if ans <= len(cards):\n",
    "            return ans\n",
    "\n",
    "        return -1\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumCardPickup(self, cards: List[int]) -> int:\n",
    "        d = dict()\n",
    "        ret = 1 << 30\n",
    "        for i in range(len(cards)):\n",
    "            if cards[i] in d:\n",
    "                ret = min(ret, i - d[cards[i]] + 1)\n",
    "            d[cards[i]] = i\n",
    "        return -1 if ret == 1 << 30 else ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumCardPickup(self, cards: List[int]) -> int:\n",
    "        mp = {}\n",
    "        ans = inf\n",
    "        for i, x in enumerate(cards):\n",
    "            if x in mp:\n",
    "                ans = min(ans, i - mp[x] + 1)\n",
    "            mp[x] = i\n",
    "        return ans if ans != inf else -1\n",
    "            \n",
    "            \n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumCardPickup(self, cards: List[int]) -> int:\n",
    "        # hash + 滑动窗口\n",
    "        lens = len(cards)\n",
    "        left, right = 0, 0\n",
    "        window = {}\n",
    "        res = float('inf')\n",
    "        while right < lens:\n",
    "            cur = cards[right]\n",
    "            \n",
    "            if cur in window:\n",
    "                res = min(res, right - window[cur] + 1)\n",
    "                while cur in window and left < window[cur] + 1:\n",
    "                    del window[cards[left]]\n",
    "                    left += 1\n",
    "            window[cur] = right\n",
    "            right += 1\n",
    "        return res if res != float('inf') else -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def minimumCardPickup(self, cards: List[int]) -> int:\n",
    "        window = defaultdict(lambda: None)\n",
    "        res = float(\"inf\")\n",
    "        for idx, c in enumerate(cards):\n",
    "            if window[c] != None:\n",
    "                res = min(res, idx - window[c] + 1)\n",
    "                \n",
    "            window[c] = idx\n",
    "        \n",
    "        return res if res != float(\"inf\") else -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "\t\tdef minimumCardPickup(self, cards: List[int]) -> int:\n",
    "\t\t\tlastCard = dict()\n",
    "\t\t\tminVal = len(cards) + 1\n",
    "\t\t\tfor i in range(len(cards)):\n",
    "\t\t\t\tif cards[i] in lastCard:\n",
    "\t\t\t\t\tminVal = min(minVal, i - lastCard[cards[i]] + 1)\n",
    "\t\t\t\tlastCard[cards[i]] = i\n",
    "\n",
    "\t\t\tif minVal == len(cards) + 1:\n",
    "\t\t\t\treturn -1\n",
    "\t\t\telse:\n",
    "\t\t\t\treturn minVal"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumCardPickup(self, cards: List[int]) -> int:\n",
    "        ans = len(cards) + 1\n",
    "        pos = {}\n",
    "\n",
    "        for i, v in enumerate(cards):\n",
    "            if v in pos:\n",
    "                p = pos[v]\n",
    "                if i - p + 1 < ans:  # 找最近出现的 v\n",
    "                    ans = i - p + 1\n",
    "                    \n",
    "            pos[v] = i\n",
    "\n",
    "        if ans <= len(cards):\n",
    "            return ans\n",
    "\n",
    "        return -1\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumCardPickup(self, cards: List[int]) -> int:\n",
    "        ans = inf\n",
    "        mp = {}\n",
    "        for r, x in enumerate(cards):\n",
    "            if x in mp:\n",
    "                ans = min(ans, r - mp[x] + 1)\n",
    "            mp[x] = r\n",
    "        return ans if ans != inf else -1\n",
    "            \n",
    "            \n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumCardPickup(self, cards: List[int]) -> int:\n",
    "        lastDef = -len(cards)\n",
    "        lastPosMap = defaultdict(lambda: lastDef)\n",
    "        result = -lastDef + 1\n",
    "        for i, n in enumerate(cards):\n",
    "            last = lastPosMap[n]\n",
    "            result = min(result, i - last + 1)\n",
    "            lastPosMap[n] = i\n",
    "        return -1 if result > len(cards) else result\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumCardPickup(self, cards: List[int]) -> int:\n",
    "        dic={}\n",
    "        ans=1111111110\n",
    "        for i in range(len(cards)):\n",
    "            if cards[i] in dic:\n",
    "                ans=min(ans,i-dic[cards[i]]+1)\n",
    "                dic[cards[i]]=i\n",
    "            else:\n",
    "                dic[cards[i]]=i\n",
    "        return ans if ans!=1111111110  else -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumCardPickup(self, cards: List[int]) -> int:\n",
    "        dis=1000001\n",
    "        p = [-1 for i in range(dis)]\n",
    "        for i in range(len(cards)):\n",
    "            if p[cards[i]]!=-1:\n",
    "                dis = min(dis,i-p[cards[i]])\n",
    "            p[cards[i]]=i\n",
    "        if dis==1000001:\n",
    "            return -1\n",
    "        else:\n",
    "            return dis+1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumCardPickup(self, cards: List[int]) -> int:\n",
    "        dt=dict()\n",
    "        ans=inf\n",
    "        for i in range(len(cards)):\n",
    "            card = cards[i]\n",
    "        \n",
    "        \n",
    "            if card not in dt:\n",
    "                dt[card]=[i]\n",
    "            else:\n",
    "                ans=min(ans,i-dt[card][-1]+1)\n",
    "                dt[card].append(i)\n",
    "        if ans==inf:\n",
    "            return -1\n",
    "        return ans\n",
    "                    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumCardPickup(self, cards: List[int]) -> int:\n",
    "        d = dict()\n",
    "        for i, q in enumerate(cards):\n",
    "            if q in d.keys():\n",
    "                d[q].append(i)\n",
    "            else:\n",
    "                d[q] = [i]\n",
    "        mn = -1\n",
    "        for x in d.values():\n",
    "            if len(x) > 1:\n",
    "                t = min([x[i+1]-x[i]+1 for i in range(len(x)-1)])\n",
    "                if mn == -1:\n",
    "                    mn = t\n",
    "                else:\n",
    "                    mn = min(mn, t)\n",
    "        return mn"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from collections import defaultdict\n",
    "\n",
    "class Solution:\n",
    "    def minimumCardPickup(self, cards: List[int]) -> int:\n",
    "        dic = defaultdict(list)\n",
    "        res = float('inf')\n",
    "        for i, num in enumerate(cards):\n",
    "            if num in dic:\n",
    "                res = min(res, i - dic[num][-1] + 1)\n",
    "            dic[num].append(i)\n",
    "        if res == float('inf'):\n",
    "            return -1\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumCardPickup(self, cards: List[int]) -> int:\n",
    "        from collections import defaultdict\n",
    "        counts = defaultdict(list)\n",
    "        for i,c in enumerate(cards):\n",
    "            counts[c].append(i)\n",
    "        res = float(\"inf\")\n",
    "        for v in counts.values():\n",
    "            if len(v) < 2:\n",
    "                continue\n",
    "            i = 0\n",
    "            while i < len(v) - 1:\n",
    "                cur = v[i+1]-v[i]+1\n",
    "                res = min(cur,res)\n",
    "                i += 1\n",
    "        if res == float(\"inf\"):\n",
    "            return -1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumCardPickup(self, cards: List[int]) -> int:\n",
    "        mp=defaultdict(list)\n",
    "        for i,v in enumerate(cards):\n",
    "            mp[v].append(i)\n",
    "        ans=inf \n",
    "        for x in mp:\n",
    "            for i in range(len(mp[x])-1):\n",
    "                ans=min(ans,mp[x][i+1]-mp[x][i]+1)\n",
    "        if ans<inf:\n",
    "            return ans\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumCardPickup(self, cards: List[int]) -> int:\n",
    "        que = defaultdict(list)\n",
    "        ans = inf\n",
    "        for i, j in enumerate(cards):\n",
    "            if que[j] and i - que[j][-1] + 1 < ans:\n",
    "                ans = i - que[j][-1] + 1\n",
    "            que[j].append(i)\n",
    "        return ans if ans != inf else -1\n",
    "         \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumCardPickup(self, cards: List[int]) -> int:\n",
    "        min = -1\n",
    "        temp = defaultdict(list)\n",
    "        for i in range(len(cards)):\n",
    "            if cards[i] in temp:\n",
    "                if min == -1:\n",
    "                    min = i-temp[cards[i]][-1] + 1\n",
    "                elif i-temp[cards[i]][-1] + 1 == 2:\n",
    "                    return 2\n",
    "                elif i-temp[cards[i]][-1] + 1 < min:\n",
    "                    min = i-temp[cards[i]][-1] + 1\n",
    "            temp[cards[i]].append(i)\n",
    "        return min"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
